package work.xiaohong.mina.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import work.xiaohong.mina.message.GeneralMessage;
import work.xiaohong.mina.model.Result;
import work.xiaohong.mina.service.SocketService;

/**
 * @author Kevin
 * @data 3/2/2022 8:01 PM
 * @description socket接口
 **/
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/socket")
public class SocketController {

  @Autowired
  private SocketService socketService;


  /**
   * 创建PLC客户端
   * @param ip
   * @param port
   * @return
   */
  @Operation(summary = "创建PLC客户端",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
  @GetMapping("/plc/client/creat")
  public Result plcClientCreat(@RequestParam(name = "ip") String ip, @RequestParam(name = "port") Integer port){

    return socketService.plcClientCreat(ip,port);
  }


  /**
   * 创建PLC服务端
   * @param port
   * @return
   */
  @Operation(summary = "创建PLC服务端",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
  @GetMapping("/plc/server/creat")
  public Result plcServerCreat(@RequestParam(name = "port") Integer port){

    return socketService.plcServerCreat(port);
  }


  /**
   * 指定PLC客户端发送消息
   * @param ip
   * @param port
   * @param message
   * @return
   */
  @Operation(summary = "指定PLC客户端发送消息",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
  @PostMapping("/plc/client/send")
  public Result plcClientSend(@RequestParam(name = "ip") String ip,@RequestParam(name = "port") Integer port, @RequestBody GeneralMessage message){

    return socketService.sendByPlcClient(ip,port,message);
  }

//  /**
//   * 指定PLC服务端发送消息
//   * @param port
//   * @param message
//   * @return
//   */
//  @Operation(summary = "指定PLC服务端发送消息",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
//  @PostMapping("/plc/server/send")
//  public Result plcServerSend(@RequestParam(name = "port") Integer port, @RequestBody GeneralMessage message){
//
//    return socketService.sendByPlcServer(port,message);
//  }

  /**
   * 关闭指定的PLC客户端
   * @param ip
   * @param port
   * @return
   */
  @Operation(summary = "关闭指定的PLC客户端",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
  @GetMapping("/plc/client/close")
  public Result plcClientClose(@RequestParam(name = "ip") String ip,@RequestParam(name = "port") Integer port){

    return socketService.plcClientClose(ip,port);

  }


//  /**
//   * 关闭指定的PLC服务端
//   * @param port
//   * @return
//   */
//  @Operation(summary = "关闭指定的PLC服务端",description = "所需权限：",security ={@SecurityRequirement(name ="Authorization")})
//  @GetMapping("/plc/server/close")
//  public Result plcServerClose(@RequestParam(name = "port") Integer port){
//
//    return socketService.plcServerClose(port);
//
//  }



}
